package Jets.utils.TTS
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	public class TextSpeech extends EventDispatcher 
	{
		static protected const TTS_BAIDU:String = "http://tts.baidu.com/text2audio?lan=";
		static protected const TTS_GOOGLE:String = "http://translate.google.com/translate_tts?&ie=UTF-8&oe=UTF-8&tl=";
		
		static public var LanguageCode:String = "zh";
		
		private var ms_nURL:String = "";
		private var ms_nIndex:int = 0;
		private var ms_nCallBack:Function = null;
		private var ms_nSound:Sound;
		private var ms_nSoundChannel:SoundChannel;
		private var ms_nTextList:Array = new Array();
		
		/**
		 * TTS在线文本朗读类
		 * @param speekType 使用朗读引擎 1表示使用百度引擎，2表示谷歌引擎
		 */
		public function TextSpeech(speekType:int = 1) 
		{
			if(speekType == 1)
			{
				ms_nURL = TTS_BAIDU + LanguageCode + "&ie=UTF-8&text=";
			}
			else
			{
				ms_nURL = TTS_GOOGLE + LanguageCode + "&q=";
			}
		}
		
		/**
		 * 朗读
		 * @param words 文本内容
		 * @param delim 分割符 默认是英文句号
		 * @param callback 朗读完成后回调函数
		 */
		public function speak(words:String,delim:String=".",callback:Function=null):void 
		{
			this.stop();
			
			ms_nTextList.length = 0;
			ms_nIndex = 0;
			ms_nCallBack = callback;
			ms_nTextList = words.split(delim);

			this.start();
		}
		
		private function start():void 
		{
			ms_nSound = new Sound();
			ms_nURL = ms_nURL + this.myUrlEncode(ms_nTextList[ms_nIndex],"utf-8");
			ms_nSound.load(new URLRequest(ms_nURL));
			ms_nSound.addEventListener(IOErrorEvent.IO_ERROR,error);
			ms_nSound.addEventListener(Event.COMPLETE,onLoadCompleted);
		}
		
		private function error(event:IOErrorEvent):void 
		{
			this.dispatchEvent(new TTSEvent(TTSEvent.SPEAK_ERROR));
		}
		
		private function onLoadCompleted(event:Event):void 
		{
			ms_nSound.removeEventListener(Event.COMPLETE,onLoadCompleted);
			this.play();
		}
		
		private function onPlayCompleted(event:Event):void 
		{
			ms_nSoundChannel.removeEventListener(Event.SOUND_COMPLETE,onPlayCompleted);
			ms_nIndex++;
			if(ms_nIndex > ms_nTextList.length - 1) 
			{
				this.dispatchEvent(new TTSEvent(TTSEvent.SPEAK_COMPLETE));
				if(ms_nCallBack)
				{
					ms_nCallBack();
				}
				return;
			}
			this.start();
		}
		
		private function myUrlEncode(str:String,code:String):String
		{
			var stringresult:String = "";
			var byte:ByteArray =new ByteArray();
			byte.writeMultiByte(str,code);
			for (var i:int; i<byte.length; i++)
			{
				stringresult +=  escape(String.fromCharCode(byte[i]));
			}
			return stringresult;
		}
		
		private function play():void 
		{ 
			ms_nSoundChannel = ms_nSound.play();
			ms_nSoundChannel.addEventListener(Event.SOUND_COMPLETE,onPlayCompleted);
		}
		
		public function stop():void 
		{
			if(ms_nSoundChannel)
			{
				if(ms_nSoundChannel.hasEventListener(Event.SOUND_COMPLETE))
				{
					ms_nSoundChannel.removeEventListener(Event.SOUND_COMPLETE,onPlayCompleted);
				}
				ms_nSoundChannel.stop();
				ms_nSoundChannel=null;
				if(ms_nSound.hasEventListener(Event.COMPLETE))
				{
					ms_nSound.removeEventListener(Event.COMPLETE,onLoadCompleted);
				}
			}
		}
		public function stopAll():void
		{
			this.stop();
			ms_nTextList.length = 0;
		}

		public function repeat():void 
		{
			if(ms_nTextList.length == 0)
				return;
			this.stop();
			ms_nIndex=0;
			this.start();
		}

	}
}

